﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace ERP.Manager.Controllers.CustomAttributes
{
    public class CheckLoginAttribute : ActionFilterAttribute
    {
        public CheckLoginAttribute() { }


        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!HasData(filterContext))
            {
         
                ErrorRedirect(filterContext);
                return;
            }
        }


        private void ErrorRedirect(ActionExecutingContext filterContext)
        {
            filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary(new { controller = "Home", action = "login" }));
        }



        private bool HasData(ControllerContext filterContext)
        {
            var result = filterContext.HttpContext.Session["UserId"] != null;
            return result;
        }
    }
}
